<template>
    <div class="ma">
        <common-header :user="userinfo"></common-header>
        <m-header></m-header>
        <main>
            <common-banner :banner="banner"></common-banner>
            <common-nav></common-nav>
            <div class="con_box">
                <router-view></router-view>
            </div>
        </main>
        <common-footer></common-footer>
    </div>
</template>
<script>
import CommonHeader from '@/components/commonheader2'
import CommonFooter from '@/components/commonfooter'
import CommonBanner from '@/components/commonBanner'
import commonNav from '@/components/commonNav2'
import mHeader from '@/components/mobilenav'
export default {
    data(){
        return{
            userinfo:null,
            banner:[],
            ismobile:false,
            wid:'',
            token:''
        }
    },
    created(){
            this.wid = document.body.clientWidth
            if(this.wid<900){
            this.ismobile = true
            this.getmobile()
            }else{
                this.getdata()
                this.ismobile = false
            }
        },
    mounted(){
        let token  = localStorage.getItem('token')
        // if(token){
        //     this.getuserInfo(token)
        // }else{
        //     this.$router.push({
        //         path:'/login'
        //     })
        // }
        this.getdata()
    },
    watch:{
        
        token:{
            handler(newval,oldval){
                if(newval){
                    this.getuserInfo(newval)
                }
            },
            deep:true,
            immediate:true
        },
    },
    methods:{
        getuserInfo(token){
            this.$api.get('getUserInfo',{
                headers:{
                    Authorization:token
                }
            }).then(res=>{
                if(res.code === 200){
                    localStorage.setItem('userInfo',JSON.stringify(res.data.data))
                    this.userinfo = res.data.data
                    console.log(this.userinfo)
                }else{
                    alert(res.message)
                    localStorage.removeItem('token')
                    localStorage.removeItem('userInfo')
                    this.$router.push({path:'/login'})
                }
            })
        },
        getdata(){
            this.$api.post('getAdList',{
                "sort":"sort",
                "limit":100, 
                "group":"创客空间banner" 
            }).then(res=>{
                this.banner = res.data.list
            })
        },
        getmobile(){
            this.$api.post('getAdList',{
                "sort":"sort",
                "limit":100, 
                "group":"创客空间banner手机端" 
            }).then(res=>{
                this.banner = res.data.list
            })
        },
    },
    components:{
        CommonBanner,
        CommonFooter,
        CommonHeader,
        commonNav,
        mHeader
    }
}
</script>
<style lang="scss" scoped>
.con_box{
    width: 1200px;
    margin: 0 auto;
}
@media screen and (max-width: 900px) {
    .con_box{
        width: 100%;
        margin: 0 auto;
        padding: 0 .3rem;
        box-sizing: border-box;
    }
    .ma{
        padding-bottom: 1.2rem;
    }
}
</style>